#! /usr/bin/env python3
# -*- coding: utf-8 -*-
# __author__ = "life"
# Email: 379960530@qq.com
# Date: 17-1-31

import json
import paramiko
import threading

class Remotehost(object):
    # 远程主机操作类

    def __init__(self,host,port,username,password,cmd):
        self.host = host
        self.port = port
        self.username = username
        self.password = password
        self.cmd = cmd

    def command(self):
        # 获取命令
        ssh = paramiko.SSHClient()
        ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
        ssh.connect(hostname=self.host,port=self.port,username=self.username,password=self.password)
        stdin,stdout,stderr = ssh.exec_command(self.cmd) # 获取命令结果
        res,err = stdout.read(),stderr.read()
        result = res if res else err
        print('[%s]'.center(50,'-')%self.host)
        print(result.decode())
        ssh.close()

    def put(self):
        """上传"""

        try:
            transport = paramiko.Transport((self.host,self.port))
            transport.connect(username=self.username,password=self.password)
            sftp = paramiko.SFTPClient.from_transport(transport)
            sftp.put(self.cmd.split()[1],self.cmd.split()[2]) # 上传文件

            transport.close()
            print('\033[32;0m[%s] 上传文件[%s] 成功...\033[0m'%(self.host,self.cmd.split()[2]))
        except Exception as err:
            print('\033[31;0m错误:[%s] [%s] \033[0m'%(self.host,err))

    def run(self):
        """反射"""
        cmd_str = self.cmd.split()[1]
        if hasattr(self,cmd_str):
            getattr(self,cmd_str)()
        else:
            setattr(self,cmd_str,self.command)
            getattr(self,cmd_str)()






if __name__ == '__main__':
    with open('database','r') as file:
        data_dict = json.loads(file.read()) # 获取数据
    for k in data_dict:   # 打印地址组
        print(k)
    group_choice = input('Please input the group:').strip()
    if data_dict.get(group_choice):
        host_dict = data_dict[group_choice]   # 主机字典
        for k in host_dict:     # 打印主机
            print(k)

        while True:
            cmd = input('选择进行操作的命令:').strip()
            thread_list=[]
            if cmd:
                for k in host_dict:
                    host,port,username,password=k,host_dict[k]['port'],host_dict[k]['username'],host_dict[k]['password']
                    func = Remotehost(host,port,username,password,cmd) #实例化类
                    t = threading.Thread(target=func.run) #创建线程
                    t.start()
                    thread_list.append(t)
                for i in thread_list:
                    i.join()
    else:
        print("\033[31;0m操作组不存在\033[0m")